//
//  NewsViewController.m
//  SportSocial
//
//  Created by yull on 14/11/19.
//  Copyright (c) 2014年 yull. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsChildViewController.h"
#import "KnowLedgeViewController.h"
#import "BMapKit.h"
#import "LocationManager.h"
#import "RunViewController.h"

#define KnowLedge_Page_Size 10

@interface NewsViewController () <BMKMapViewDelegate>

@property (nonatomic, strong) NewsChildViewController *newsChildViewController;
@property (nonatomic, strong) KnowLedgeViewController *knowLedgeViewController;

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIButton *newsButton;
@property (nonatomic, strong) UIButton *knowLedgeButton;

@property (nonatomic, strong) UIView *viewMap;
@property (nonatomic, strong) BMKMapView *mapView;

@property (nonatomic, assign) BOOL haveSetCenter;

@end

@implementation NewsViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initNaviItem];
    [self initTopButton];
    
    [self onNewsButtonClicked:self.newsButton];
    
    [self initMapView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didUpdateBMKUserLocation:) name:@"UserLocationDidChange" object:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    
    self.topView.hidden = NO;
}

//- (void)viewDidAppear:(BOOL)animated
//{
//    [super viewDidAppear:animated];
//    self.navigationController.navigationBarHidden = NO;
//    
//    self.topView.hidden = NO;
//}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.topView.hidden = YES;
}

- (void)initNaviItem
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 28, 26);
    [leftButton setImage:[UIImage imageNamed:@"title_icon1.png"] forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:@"title_icon1_selection.png"] forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(showLeft:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    
    self.navigationItem.leftBarButtonItem = leftItem;
}

- (void)showLeft:(id)sender
{
    [self.sideMenuViewController presentLeftMenuViewController];
}

- (void)initTopButton
{
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 7, 140, 30)];
    self.topView.centerX = self.view.width / 2;
    [self.navigationController.navigationBar addSubview:self.topView];
    
    self.newsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.newsButton.frame = CGRectMake(0, 0, self.topView.width/2, self.topView.height);
    [self.newsButton setTitle:@"新闻" forState:UIControlStateNormal];
    [self.newsButton setTitleColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.5] forState:UIControlStateNormal];
    [self.newsButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [self.newsButton setBackgroundImage:[[UIImage imageNamed:@"title_button1.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:10] forState:UIControlStateNormal];
    [self.newsButton setBackgroundImage:[[UIImage imageNamed:@"title_button1_selection.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:10] forState:UIControlStateSelected];
    self.newsButton.titleLabel.font = [UIFont boldSystemFontOfSize:14.0f];
    [self.topView addSubview:self.newsButton];
    
    self.knowLedgeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.knowLedgeButton.frame = CGRectMake(self.topView.width/2, 0, self.topView.width/2, self.topView.height);
    [self.knowLedgeButton setTitle:@"小知识" forState:UIControlStateNormal];
    [self.knowLedgeButton setTitleColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.5] forState:UIControlStateNormal];
    [self.knowLedgeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [self.knowLedgeButton setBackgroundImage:[[UIImage imageNamed:@"title_button2.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:10] forState:UIControlStateNormal];
    [self.knowLedgeButton setBackgroundImage:[[UIImage imageNamed:@"title_button2_selection.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:10] forState:UIControlStateSelected];
    self.knowLedgeButton.titleLabel.font = [UIFont boldSystemFontOfSize:14.0f];
    [self.topView addSubview:self.knowLedgeButton];
    
    [self.newsButton addTarget:self action:@selector(onNewsButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.knowLedgeButton addTarget:self action:@selector(onKnowLedgeButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (NewsChildViewController *)newsChildViewController
{
    if (!_newsChildViewController) {
        CGRect rect = CGRectMake(0, 0, self.view.width, self.view.height - Tabbar_Height);
        self.newsChildViewController = [[NewsChildViewController alloc] init];
        _newsChildViewController.view.frame = rect;
        [self addChildViewController:_newsChildViewController];
        [_newsChildViewController willMoveToParentViewController:self];
        [self.view addSubview:_newsChildViewController.view];
    }
    _newsChildViewController.tableViewNews.scrollsToTop = YES;
    if (_knowLedgeViewController) {
        _knowLedgeViewController.tableViewKnowLedge.scrollsToTop = NO;
    }
    return _newsChildViewController;
}

- (KnowLedgeViewController *)knowLedgeViewController
{
    if (!_knowLedgeViewController) {
        CGRect rect = CGRectMake(0, 0, self.view.width, self.view.height - Tabbar_Height);
        self.knowLedgeViewController = [[KnowLedgeViewController alloc] init];
        _knowLedgeViewController.view.frame = rect;
        [self addChildViewController:_knowLedgeViewController];
        [_knowLedgeViewController willMoveToParentViewController:self];
        [self.view addSubview:_knowLedgeViewController.view];
    }
    _knowLedgeViewController.tableViewKnowLedge.scrollsToTop = YES;
    if (_newsChildViewController) {
        _newsChildViewController.tableViewNews.scrollsToTop = NO;
    }
    return _knowLedgeViewController;
}

- (void)onNewsButtonClicked:(id)sender
{
    self.newsButton.selected = YES;
    self.knowLedgeButton.selected = NO;
    [self.view bringSubviewToFront:self.newsChildViewController.view];
    [self.view bringSubviewToFront:self.viewMap];
}

- (void)onKnowLedgeButtonClicked:(id)sender
{
    self.knowLedgeButton.selected = YES;
    self.newsButton.selected = NO;
    [self.view bringSubviewToFront:self.knowLedgeViewController.view];
    [self.view bringSubviewToFront:self.viewMap];
}

- (void)initMapView
{
    CGRect rect = CGRectMake(0, self.view.height - Tabbar_Height - 56, self.view.width, 120 + 38);
    self.viewMap = [[UIView alloc] initWithFrame:rect];
    self.viewMap.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [self.view addSubview:self.viewMap];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.viewMap.width - 85, 0, 56, 56)];
    imageView.image = [UIImage imageNamed:@"run_icon.png"];
    imageView.userInteractionEnabled = YES;

    UISwipeGestureRecognizer *swipeUpGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeViewMap:)];
    [swipeUpGesture setDirection:UISwipeGestureRecognizerDirectionUp];
    [imageView addGestureRecognizer:swipeUpGesture];
    UISwipeGestureRecognizer *swipeDownGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeViewMap:)];
    [swipeDownGesture setDirection:UISwipeGestureRecognizerDirectionDown];
    [imageView addGestureRecognizer:swipeDownGesture];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToRunView:)];
    [tapGesture requireGestureRecognizerToFail:swipeUpGesture];
    [tapGesture requireGestureRecognizerToFail:swipeDownGesture];
    [imageView addGestureRecognizer:tapGesture];
    
    [self.viewMap addSubview:imageView];
}

- (void)didUpdateBMKUserLocation:(NSNotification *)notification
{
    if (!self.mapView) {
        return;
    }
    
    CLLocation *location = notification.object;
    [self changeMapViewRegionWithLocation:location];
}

- (void)changeMapViewRegionWithLocation:(CLLocation *)location
{
    CLLocationCoordinate2D coordinate = location.coordinate;
    BMKCoordinateSpan span = BMKCoordinateSpanMake(0.05f, 0.05f);
    BMKCoordinateRegion region = BMKCoordinateRegionMake(coordinate, span);

    if (!self.haveSetCenter) {
        self.haveSetCenter = YES;
        [self.mapView setRegion:region];
    }
    
    
    [self.mapView removeAnnotations:self.mapView.annotations];
    
    BMKPointAnnotation *pointAnnotation = [[BMKPointAnnotation alloc] init];
    pointAnnotation.coordinate = location.coordinate;
    [self.mapView addAnnotation:pointAnnotation];
}

#pragma mark -
#pragma mark - UIGesture
- (void)swipeViewMap:(UISwipeGestureRecognizer *)swipeGesture
{
    if (swipeGesture.direction == UISwipeGestureRecognizerDirectionDown) {
        [UIView animateWithDuration:0.3 animations:^{
            self.viewMap.top = self.view.height - Tabbar_Height - 56;
            self.mapView.top = 56;
        }];
    } else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionUp) {
        if (!self.mapView) {
            self.mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 28, self.view.width, 120)];
            self.mapView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
            self.mapView.delegate = self;
            [self.viewMap addSubview:self.mapView];
            [self.viewMap sendSubviewToBack:self.mapView];
            
            CLLocation *location = [LocationManager shareInstance].location;
            
            if (location) {
                [self changeMapViewRegionWithLocation:location];
            }
        }
        
        [UIView animateWithDuration:0.3 animations:^{
            self.viewMap.top = self.view.height - Tabbar_Height - 120 - 28;
            self.mapView.top = 28;
        }];
    }
}

- (void)tapToRunView:(UITapGestureRecognizer *)tapGesture
{
    RunViewController *runViewController = [[RunViewController alloc] init];
    runViewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:runViewController animated:YES];
}

#pragma mark - BMKMapViewDelegate
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKAnnotationView *annotationView = [[BMKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MyAnnotation"];
        annotationView.image = [UIImage imageNamed:@"news_map_me"];
        
        UILabel *label = [[UILabel alloc] initWithFrame:annotationView.bounds];
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont boldSystemFontOfSize:12.0f];
        label.textColor = [UIColor whiteColor];
        label.text = @"我";
        label.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        label.centerY -=5;
        label.centerX -=2;
        [annotationView addSubview:label];
        return annotationView;
    }
    return nil;
}
@end
